angular.module('versionEditApp', []).controller( 'versionEditCtr',
	function($scope, $http) {
	
	$scope.id = angular.element($("#id")).val();  //获取id
 
	$http({  
		url:basePath+'/system/queryVersion',  
		params: {  
			'id':$scope.id  
	    },  
		method:'POST'  
		}).success(function(data,header,config,status){ 
			
			$scope.versionName = data[0].versionName;  
			$scope.districtName = data[0].districtName; 
			$scope.districtCode = data[0].districtCode; 
			$scope.versionDesc = data[0].versionDesc; 
			$scope.versionState = data[0].versionState;
			$scope.models=data[0].versionPass; 
			$scope.organizeName = data[0].organizeName; 
			$scope.organizeId = data[0].organizeId;
			if($scope.models != null && $scope.models.indexOf("index_districtIndex") !=-1){
				$("#type1").prop("checked",true);
				$scope.type1='index_districtIndex';
			}else{
				$scope.type1='0';
			}
			if($scope.models != null && $scope.models.indexOf("trend") !=-1){
				$("#type2").prop("checked",true);
				$scope.type2='trend';
			}else{
				$scope.type2='0';
			}
			if($scope.models != null && $scope.models.indexOf("comparative") !=-1){
				$("#type3").prop("checked",true);
				$scope.type3='comparative';
			}else{
				$scope.type3='0';
			}
			if($scope.models != null && $scope.models.indexOf("difference") !=-1){
				$("#type4").prop("checked",true);
				$scope.type4='difference';
			}else{
				$scope.type4='0';
			}
			if($scope.models != null && $scope.models.indexOf("thermal") !=-1){
				$("#type5").prop("checked",true);
				$scope.type5='thermal';
			}else{
				$scope.type5='0';
			}
			if($scope.models != null && $scope.models.indexOf("stock") !=-1){
				$("#type6").prop("checked",true);
				$scope.type6='stock';
			}else{
				$scope.type6='0';
			}
			if($scope.models != null && $scope.models.indexOf("export") !=-1){
				$("#type7").prop("checked",true);
				$scope.type7='export';
			}else{
				$scope.type7='0';
			}
			if($scope.models != null && $scope.models.indexOf("industryReport") !=-1){
				$("#type8").prop("checked",true);
				$scope.type8='industryReport';
			}else{
				$scope.type8='0';
			}
			if($scope.models != null && $scope.models.indexOf("directreport") !=-1){
				$("#type9").prop("checked",true);
				$scope.type9='directreport';
			}else{
				$scope.type9='0';
			}
		}).error(function(data,header,config,status){  
			  toastr.warning("操作失败！");  
  		});
			
		// 点击保存按钮，新增一条数据
		$scope.editVersion = function() {
			// form表单的验证 数据不对，return false
			var formValid = true;
			if (!$scope.versionName) {
				toastr.warning("版本不可为空！");
				formValid = false;
				return false;
			}
			if (formValid) { // 表单通过验证后提交
				submitData(); 
			}
		};

		
		// form表单提交方法 submit(若是校验都通过啦，数据便可以提交啦)
		function submitData() {
		 $scope.versionPass = $scope.type1+"_"+$scope.type2+"_"+$scope.type3+"_"+$scope.type4+"_"+$scope.type5+"_"+$scope.type6+"_"+$scope.type7+"_"+$scope.type8+"_"+$scope.type9;
		 
			var form = $("form[name=versionFormEdit");  
			var options  = {
				url:basePath+'/system/updateVersion',
                type:'post',  
                data:{
                	'versionName' : $scope.versionName,
					'districtCode':$scope.districtCode,
					'versionPass':$scope.versionPass,
					'versionDesc':$scope.versionDesc,
					'versionState':$scope.versionState
                },  
                success:function(data){  
                   if(data){ 
                           toastr.success('操作数据成功!'); 
                           setTimeout(function() { 
                        	   location.href = basePath+'/system/versionApply';
                           }, 500); 
                     }else{ 
                          toastr.error('操作数据失败!'); 
                          setTimeout(function() {
                        	  location.href = basePath+'/system/versionApply';
          				}, 500);
                     } 

            	}  
        };  
        form.ajaxSubmit(options);  
	}
		
		//  点击取消按钮，跳转到查询页面
		$scope.backHis = function() {
			location.href = basePath+'/system/versionApply';
		};
});
